开发者

how to get value?

开发者 https://www.devze.com 2023-01-28 04:31 出处:网络
How to get current selected value of a dropdown which is inside div? divid is divcontent and dropdownid is country

How to get current selected value of a dropdown which is inside div? divid is divcontent and dropdownid is country

code follows

<form id="frm1">

both dropdowns are aj开发者_开发百科ax-generated based on some conditions.

Thanks in advance


Just use .val() to get the value of any input type element, include <select> elements. Since IDs are unique you can just do this:

var value = $("#country").val();


And with the same technique, you are able to select a value:

<select>
  <option value="germany">Germany</option>
  <option value="france">France</option>
</select>

$("#country").val("germany");


When you use jQuery noconflict mode then just use like so:

<form id='myForm1'>
<input name='username' id='username' value='WhatEver'>
<select name='YesNo' id='YesNo'>
    <option value='0'>No</option>
    <option value='1'>Yes</option>
</select>
</form>

var uname = jQuery("#username").val();
var confirm = jQuery("#YesNo").val();

console.log(uname);

returns: WhatEver

console.log(confirm);

returns: 1 (if Yes was selected)

But if you want get all form items at same time then use it like so:

var formData = jQuery("#myForm1").serialize();
console.log(formData);

returns: username=WhatEver&YesNo=1

0

精彩评论

暂无评论...
验证码 换一张
取 消