$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - How to assign a var value to jQuery .val() - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to assign a var value to jQuery .val() - Stack Overflow

matteradmin15PV0评论

I get value from checkFields method and then I assign it to hidden variable searchValidate. The value of atleastOneFilled will be true or false .Below is the code

var atleastOneFilled = checkFields($("#searchForm"));
alert('atleastOneFilled' + atleastOneFilled );
$('#searchValidate').val(atleastOneFilled);  

But the value is not getting set to searchValidate. The alert displays the correct value but still not assigned to searchValidate. Is it something a wrong way of assigning to .val()

HTML:

<input type="hidden" value="" th:name="searchValidate"/>

I get value from checkFields method and then I assign it to hidden variable searchValidate. The value of atleastOneFilled will be true or false .Below is the code

var atleastOneFilled = checkFields($("#searchForm"));
alert('atleastOneFilled' + atleastOneFilled );
$('#searchValidate').val(atleastOneFilled);  

But the value is not getting set to searchValidate. The alert displays the correct value but still not assigned to searchValidate. Is it something a wrong way of assigning to .val()

HTML:

<input type="hidden" value="" th:name="searchValidate"/>
Share Improve this question edited Jun 9, 2015 at 3:43 Java_User asked Jun 9, 2015 at 3:38 Java_UserJava_User 4753 gold badges12 silver badges30 bronze badges 5
  • What does your html look like? – Jesse Kernaghan Commented Jun 9, 2015 at 3:39
  • What type of field is #searchValidate? – Alex McMillan Commented Jun 9, 2015 at 3:40
  • What kind of element is #searchValidate? – Felix Kling Commented Jun 9, 2015 at 3:41
  • How do you check that it is not assigned? – Kirill Slatin Commented Jun 9, 2015 at 3:42
  • I have edited the question with HTML – Java_User Commented Jun 9, 2015 at 3:44
Add a ment  | 

4 Answers 4

Reset to default 2

you need to use a different jquery selector:

$('input[name="searchValidate"]').val(atLeastOneField);

the # signifies the id property of an element

The issue is happening because you are using id selector in jQuery to set the value and the input element is missing the id attribute. So please change the html as shown below and it will work

<input type="hidden" value="" id="searchValidate" th:name="searchValidate"/>

I think you can't select when attributes contain ":" you need to use foreach loop and match attributes .

 var atleastOneFilled = checkFields($("#searchForm"));
    alert('atleastOneFilled' + atleastOneFilled );

$( "input[type='hidden']" ).each(function(){

if($( "input" ).attr("th:name")="searchValidate")
{
    $('#searchValidate').val(atleastOneFilled);  

}

});

maybe try

$('#searchValidate').text(atleastOneFilled); 

instead of

$('#searchValidate').val(atleastOneFilled); 
Post a comment

comment list (0)

  1. No comments so far