2015-03-30 78 views
0

我有一個jQuery代碼,它在選擇下拉列表中的選項時顯示div。它工作正常,但不是我所需要的。問題在於,在選擇該選項後,頁面會刷新,因爲選擇上有onchange="submit()"。這使得在div出現約半秒,當頁面加載選擇選項後顯示div。 onchange =「submit()」使它無法正常工作

然後再次消失

樣本HTML:

<select onchange="submit()"> 
    <option id="one" value="something">Car</option> 
    <option id="two" value="anything">Plane</option> 
</select> 

<div id="somediv" style="display:none;">This is a string.</div> 

的jQuery:

$('select').change(function(){ 
    decide($(this)); 
}); 
var decide = function (elem) { 
    var touch = elem; 
    if (touch.val() === 'anything') { 
    return $('#somediv').css('display', 'block'); 
    } 
}; 

我想的div當選擇id="two"value="anything"的選項並在頁面刷新後保持顯示時顯示。當用戶選擇其他選項時,div在頁面刷新後消失並保持隱藏狀態。我該如何做到這一點?所有的答案將不勝感激。

回答

1

您應該從onclick刪除submit()後提交調用decide功能

喜歡的東西:

<select> 
    <option id="one" value="something">Car</option> 
    <option id="two" value="anything">Plane</option> 
</select> 

<div id="somediv" style="display:none;">This is a string.</div> 

JS:

$('select').change(function(){ 
    decide($(this)); 
    $('form').submit(); 
}); 
var decide = function (elem) { 
    var touch = elem; 
    if (touch.val() === 'anything') { 
    return $('#somediv').css('display', 'block'); 
    } 
}; 
+0

謝謝你,我測試了一下,但還是同樣的問題。任何其他建議? – 2015-03-30 21:05:49

+0

你刪除了'onchange =「submit()」'? – 2015-03-30 21:13:07

+0

是的,我做到了,也清除了瀏覽器的緩存,但沒有運氣。 – 2015-03-30 21:17:04

1

我建議以下辦法:

// define the function: 
 
function showIfSelected() { 
 
    // 'this' is passed in by jQuery, and is the <select> element, 
 
    // this.options is a NodeList of the <option> elements of 
 
    // the <select>, this.selectedIndex is the index of the selected 
 
    // <option> from amongst that NodeList: 
 
    var opt = this.options[this.selectedIndex]; 
 

 
    // finding all <div> elements with a 'data-showif' attribute: 
 
    $('div[data-showif]') 
 
    // hiding them all: 
 
    .hide() 
 
    // filtering that collection of <div> elements, to find the one 
 
    // whose 'data-showif' attribute shares the value with the <option>: 
 
    .filter('[data-showif=' + opt.value + ']') 
 
    // showing that <div>: 
 
    .show(); 
 
} 
 

 
// binding the showIfSelected() function as the change-event handler: 
 
$('select').on('change', showIfSelected) 
 
// triggering the change event (to show/hide the correct <div> 
 
// on document ready: 
 
.change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select> 
 
    <option id="one" value="something">Car</option> 
 
    <option id="two" value="anything">Plane</option> 
 
</select> 
 

 
<div id="somediv" data-showif="anything">This is a string.</div>

參考文獻:

+0

謝謝你的回答,這部分工作。它顯示的div沒有提交選擇,我必須手動提交。所以,我必須做兩個手動提交,一個用於選擇,另一個用於整個頁面_(其餘部分有其他字段)_我期望自動提交選擇,然後顯示頁面加載後的div,如果選擇是第二種選擇。這可能嗎?我希望你能幫助我。再一次感謝你。 – 2015-03-30 21:51:09

1

如果你不想使用Ajax來提交你的數據,你應該通過GET/POST傳遞選擇的值或將其存儲在一個會話/ Cookie,則retrive在頁面加載時。

相反,如果使用AJAX,你可以做這樣的事情

$('select').change(function(){ 
 
    decide($(this)); 
 
    $('form[name=a]').submit(); 
 
}) 
 

 
var decide = function (elem) { 
 
    var touch = elem; 
 
    if (touch.val() === 'anything') { 
 
     return $('#somediv').css('display', 'block'); 
 
    } 
 
} 
 

 
$('form[name=a]').submit(function(){ 
 
    $('#res').text('Submitted!'); 
 
    
 
    // Put $.ajax to submit your data 
 
    return false; // Commenting this will result in a page refresh 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form name="a" method="post"> 
 
<select> 
 
    <option id="one" value="something">Car</option> 
 
    <option id="two" value="anything">Plane</option> 
 
</select> 
 
</form> 
 

 
<div id="somediv" style="display:none;">This is a string.</div> 
 
<div id="res"></div>

1

試試這個代碼,它可能工作。

<option id="one" value="First">Car</option> 
    <option id="two" value="Second" selected="selected">Plane</option> 
</select> 
<div id="div1">This is a string</div> 

的jQuery:

$(document).ready(function() { 
     $("select").change(function() { 
      var v = $("select").val(); 
      if (v == "Second") { 
       $("#div1").show(); 
      } 
      else { 
       $("#div1").hide(); 
      } 
     }); 
    }); 
+0

工作方式不同,當第二個選項被選中時,div仍然出現,這就是我想要的。但是,當選擇其他選項時,它消失的時間小於秒(頁面刷新時),然後再次顯示。 – 2015-03-30 22:18:12

+0

默認情況下,第二個選項將被選中,所以在重新加載頁面後,它會再次顯示div內容。 – 2015-03-30 22:27:32