2012-10-03 75 views
0

(可能)在這裏簡單的jQuery問題。我正在使用jQuery UI對話框,並且正在確認。基本上,當用戶選擇某個選項(結合其他情況)時,我希望能夠將它們發送回最後一次選擇的選項,或者如果我不能這樣做,那麼這是默認選項。如何從一個選擇選項發送到另一個選項?

這個選項沒有「selected」屬性,這本來就是我的例子。有沒有辦法做到這一點?

這裏是JS:

alert('hey') 
if (document.frm.notNull.checked == true) { 
    // This is where it ends! 
    if ($('input[name=valueTextField]').length > 1) { 
     $('#dialog p').empty(); 
     $('#dialog p').html("You're going to lose your stuff if you do this. You sure you want to?"); 
     $('#dialog').dialog({ 
      autoOpen: true, 
      width: 600, 
      modal: true, 
      buttons: { 
       "<spring:message code='dialogBox.cancelConfirmation.YES'/>": function() { 
        $(this).dialog("close"); 
        $("#bottom").css('visibility', 'visible'); 
        $('.edit-new').css('visibility', 'hidden'); 
        $('.edit-trash').css('visibility', 'hidden'); 
        // if table has more than one row, delete the rest. 
        deleteExtraRows('valueTable'); 
        return true; 

       }, 
       "<spring:message code='dialogBox.cancelConfirmation.NO'/>": function() { 
        $(this).dialog("close"); 

        // Need to revert the user back to where they came from here. 
        $('#control[value=1]').attr('selected', true); 
        // return false; 
       } 
      } 
     }); 
    } else { 
     $("#bottom").css('visibility', 'visible'); 
     $('.edit-new').css('visibility', 'hidden'); 
     $('.edit-trash').css('visibility', 'hidden'); 
     // if table has more than one row, delete the rest. 
     deleteExtraRows('valueTable'); 
     return true; 

    } 
}​ 

這是HTML(被動態渲染):

<div class="lbl"> 
    <label>Control</label> 
</div> 
<select style="margin:0 0 0 8px; left:15px; position:relative;" name="control" 
id="control" onChange="checkTableVisibility();"> 
    <option value=1>Check Box</option> 
    <option value=0>Dropdown Menu</option> 
    <option value=3>Radio Button</option> 
    <option value=2 selected="selected">Text Box</option> 
</select> 
+2

你有任何代碼或樣品來證明你想做的事......它有點混亂,以從上述文字看着辦吧.. – Scorpio

+0

比較遺憾的是什麼,添加了一些代碼,看它是否會幫助!感謝您的關注! – streetlight

回答

0

下面是示例代碼。希望這可以幫助你

##### Jquery Part ##### 
<script> 
$(document).ready(function(){ 
var dropdownControl =$("#control"); 
var selectedValue = dropdownControl.val(); 
dropdownControl.change(function(){ 
    var dropItem = $(this); 
    $('#dialog').dialog({ 
    autoOpen: true, 
     width: 600, 
     modal: true, 
     buttons: { 
      'Ok': function(){ 
        selectedValue = dropItem.val(); 
        $(this).dialog("close"); 
       }, 
       'Cancel' : function(){ 
        dropdownControl.val(selectedValue); 
        $(this).dialog("close"); 
       } 
       } 
    }); 
    }); 
}); 
</script> 


#### HTML ##### 
<body> 
<div> 
    <select style="margin:0 0 0 8px; left:15px; position:relative;" name="control" id="control"> 
    <option value=1>Check Box</option> 
    <option value=0>Dropdown Menu</option> 
    <option value=3>Radio Button</option> 
    <option value=2 selected="selected">Text Box</option> 
    </select> 
</div> 
<div id="dialog" style="display:none;"> 
    Do you want to change value ? 
</div> 
</body> 

我希望你能放更多的代碼。我添加了一些可能對你有幫助的代碼。請仔細閱讀與之相關的更改和評論。我將整個代碼封裝到jQuery ready函數中。請讓我知道它是否工作。

$(document).ready(function(){ 
    var dropdownControl =$("#control"); 
    var previousValue = dropdownControl.val(); 
    /// Please add above line of code on your jQuery onload section. So that previous value get set, for the first time, whenever dom gets load. so that you can use it for later cases 
alert('hey') 
if (document.frm.notNull.checked == true) { 
    // This is where it ends! 
    if ($('input[name=valueTextField]').length > 1) { 
     $('#dialog p').empty(); 
     $('#dialog p').html("You're going to lose your stuff if you do this. You sure you want to?"); 
    $('#dialog').dialog({ 
     autoOpen: true, 
     width: 600, 
     modal: true, 
     buttons: { 
      "<spring:message code='dialogBox.cancelConfirmation.YES'/>": function() { 
       $(this).dialog("close"); 
       $("#bottom").css('visibility', 'visible'); 
       $('.edit-new').css('visibility', 'hidden'); 
       $('.edit-trash').css('visibility', 'hidden'); 
       previousValue = dropdownControl.val(); 
       // please reset previousValue with the new one that you approved to change so that next time when dropdown changes it value it could use it 
       // if table has more than one row, delete the rest. 
       deleteExtraRows('valueTable'); 
       return true; 

      }, 
      "<spring:message code='dialogBox.cancelConfirmation.NO'/>": function() { 
       $(this).dialog("close"); 

       // Need to revert the user back to where they came from here. 
       $('#control[value=1]').attr('selected', true); 
       dropdownControl.val(previousValue); 
       // Please replace dropdown value with the old one if you want to revert changes. 
       // return false; 
      } 
     } 
    }); 
} else { 
    $("#bottom").css('visibility', 'visible'); 
    $('.edit-new').css('visibility', 'hidden'); 
    $('.edit-trash').css('visibility', 'hidden'); 
    // if table has more than one row, delete the rest. 
    deleteExtraRows('valueTable'); 
    return true; 

} 
}​ 
}); 
+0

這太神奇了!我遇到的唯一障礙是這裏的函數正在下拉式更改中運行。因此,當我在.ready中聲明值時,它們不會被拉入郵件函數(例如previousVal)。很奇怪,我知道! – streetlight

+0

你可以再加一些代碼嗎?我可以爲您提供示例下拉菜單。 – PRP

+0

謝謝你的幫助PRP! – streetlight

0

我想我非常理解你的問題 我的回答將是: 使用變量存儲您可以這樣做的選定值:

$("yourSelect").change(function(){ 
selected = $(this).val(); 
}); 

然後你就可以重置點擊一個按鈕值重例如

$("#reset").click(function(){ 
    $("yourSelect").val(selected); 
}); 
+0

你真的需要一個隱藏的表單域嗎?爲什麼不使用兩個函數外部的變量? – Barmar

+0

不錯,你是對的! – fatiDev

+0

現在,它更簡單 – fatiDev

相關問題