gpt4 book ai didi

javascript - jQuery - 验证表单

转载 作者:行者123 更新时间:2023-11-30 12:55:57 24 4
gpt4 key购买 nike

我试过这段代码,但它仍然提交表单,即使输入尚未经过验证。

这是我的表单代码:

<a style="cursor: pointer;" id="upload_image">Upload Image</a></li>

<?php
$attributes = array('id' => 'upload_form');
echo form_open_multipart('c=web_page&m=upload_img', $attributes);
?>

<input type='file' style="display:none;" name="photosubmit" id="photosubmit"/>
<?php echo form_cose(); ?>

这是我的 jQuery 代码:

$("#upload_image").click(function(){
$("#photosubmit").click();
if( $('photosubmit').val() != "") //
$("#upload_form").submit();
});

所以这是我的问题:

我如何确保在提交此表单之前,我的输入已经有一个值..?

最佳答案

如果你想通过 id 获取元素,你应该这样做:

if( $('#photosubmit').val() != "")

代替:

if( $('photosubmit').val() != "")

更新后的答案:
jQuery API documentation 的帮助下,我找到了一个完全可行的解决方案:

$("#the_form").submit(function(event) {
if ( $( "input:first" ).val() !== "" ) {
alert("The file input is valid.");
return;
}
alert("The file input is not valid.")
event.preventDefault();
});

https://jsfiddle.net/christianheinrichs/nbuzoxLv/2/

关于javascript - jQuery - 验证表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19121574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com