jQuery changing the name of a field
im trying to change the name of a field on the change state of a select list.
I have the following code
<开发者_运维技巧script>
$('#selectintrole').change(function(){
$('#proven_keyname').val($(this).val());
};
</script>
<select name="item_options" id="selectintrole">
<option value="20030">Universal (20030)</option>
<option value="4545456">Medium (4545456)</option>
<option value="15447">Large (15447)</option>
</select>
<input name="proven" value="1" type="checkbox" id="proven_keyname" />
It doesnt seem to be doing anything though ... when i check the generated source, nothing has changed .... am i missing something?
I think you need to bind your change event after the document is ready like so
$(function () {
$('#selectintrole').change(function () {
$('#proven_keyname').val($(this).val());
});
});
If you want to change the name attribute then replace with this
$('#proven_keyname').attr('name', $(this).val());
Your code is running before the elements exist.
Move the script below the HTML, or wrap it in $(function() { ... })
, which will run it when the page loads.
EDIT: Demo
精彩评论