2010-07-20 69 views
0

我使用jQuery我的HTML代碼如何通過默認

<input type="radio" name="maritalstatus" id="maritalstatus" value="single"/> 
    single 
<input type="radio" name="maritalstatus" id="maritalstatus" value="married"/> 
    Married 
<input type="submit" id="clicksingleactive" /> 
<input type="submit" id="clickmarriedactive"/> 

如果我提交點擊「singleactive的」按鈕來設置選擇列表和單選按鈕值,單值應該檢查,在情況下,如果我使用「clickmarriedactive」按鈕已婚值的值應該檢查如何到達這個?

<select id="state" name="state"> 
<option value="Ukrain" >Ukrain</option> 
<option value="U.S" >USA/option> 
</select> 

如果我點擊「檢查UKRAIN按鈕值應選擇選擇列表中的默認,如above.How到jQuery中實現什麼想法?

回答

0

對於第一個我寧願給單獨的id到2個差異選項已婚和單身,並使用其ID如下選擇:

<body> 
    <input type="radio" name="maritalstatus" id="maritalstatus_1" value="single"/> 
     single 
    <input type="radio" name="maritalstatus" id="maritalstatus_2" value="married"/> 
     Married 
<input type="submit" id="clicksingleactive" /> 
<input type="submit" id="clickmarriedactive"/> 
<br/> 

</body> 
<script> 
    $(document).ready(function() 
    { 
     $("#clicksingleactive").click(function() 
     { 
      $("#maritalstatus_1").attr("checked","checked"); 
     }); 
     $("#clickmarriedactive").click(function() 
     { 
      $("#maritalstatus_2").attr("checked","checked"); 
     }); 
    }); 
</script> 
0

SELECT LIST

$("#state").val("Ukrain"); Sets the Ukrain List 
$("#state").val("U.S"); Sets the USA default 
1

首先,你應該改變你的單選按鈕的ID。沒有兩個元素可以具有相同的ID。 (但保留名稱相同,這就是團體兩個選項。)

<input type="radio" name="maritalstatus" id="maritalstatus1" value="single"/> 
    single 
<input type="radio" name="maritalstatus" id="maritalstatus2" value="married"/> 
    Married 

然後只需添加單擊處理這樣的:

$('#clicksingleactive').click(function() { 
    $('#maritalstatus1').attr('checked', true); 
}); 
$('#clickmarriedactive').click(function() { 
    $('#maritalstatus2').attr('checked', true); 
}); 
0

類似的東西? (不知道我是否完全理解你的問題)

<html> 
<head> 
    <title>S.O. 3287253</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     $('#clicksingleactive').click(function() { 
      $('#maritalstatus-single').attr('checked', 'checked'); 
     }); 
     $('#clickmarriedactive').click(function() { 
      $('#maritalstatus-married').attr('checked', 'checked'); 
     }); 
     $('#selectUkrain').click(function() { 
      $('#state').val('Ukrain'); 
     }); 

    }); 

    </script> 
</head> 
<body> 
<form id="maritalstatus"> 
    <input type="radio" name="maritalstatus" id="maritalstatus-single" value="single"/> single 
    <input type="radio" name="maritalstatus" id="maritalstatus-married" value="married"/> Married 
    <select id="state" name="state"> 
     <option value="" selected="selected">== choose ==</option> 
     <option value="Ukrain">Ukrain</option> 
     <option value="U.S">USA/option> 
    </select> 
    <br/> 
    <input type="button" id="clicksingleactive" value="Single"/> 
    <input type="button" id="clickmarriedactive" value="Married"/> 
    <input type="button" id="selectUkrain" value="Ukrain"/> 
</form> 
</body> 
</html>