Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用javascript访问imgsrc_Javascript_Jquery_Forms - Fatal编程技术网

用javascript访问imgsrc

用javascript访问imgsrc,javascript,jquery,forms,Javascript,Jquery,Forms,^这个代码不适合我。我希望有一个表单在占位符img仍然存在时不提交。我的代码犯了什么错误 HTML: 可以使用正则表达式确认src包含placeholder.png <form method="post" action="/{{ curr_user }}" enctype="multipart/form-data" onSubmit="return validate(this);" > <div class="submit-image"> &l

^这个代码不适合我。我希望有一个表单在占位符img仍然存在时不提交。我的代码犯了什么错误

HTML:



可以使用正则表达式确认src包含placeholder.png

<form method="post" action="/{{ curr_user }}" enctype="multipart/form-data" onSubmit="return validate(this);" >

    <div class="submit-image">
        <img id="img" src="../static/img/placeholder.png" onerror="this.src = '../static/img/placeholder.png'" alt="submitted image" />
    </div>
    <span class="input-url text">
        <input type="text" id="url" placeholder="http://" maxlength="320"/>
    </span>
    <div class="form-actions">
        <input type="submit" id="submit" name="op" value="Submit" class="form-submit"/
    </div>

</form>
还有一点。你的代码在这里

if( ( /placeholder\.png$/.test( $('#img').attr('src') ) ) {
        return false;
}


您正在OneError事件上加载相同的图像,这将尝试加载相同的图像并再次触发OneError事件,并导致您进入无限循环。

可能使用正则表达式确认src包含placeholder.png

<form method="post" action="/{{ curr_user }}" enctype="multipart/form-data" onSubmit="return validate(this);" >

    <div class="submit-image">
        <img id="img" src="../static/img/placeholder.png" onerror="this.src = '../static/img/placeholder.png'" alt="submitted image" />
    </div>
    <span class="input-url text">
        <input type="text" id="url" placeholder="http://" maxlength="320"/>
    </span>
    <div class="form-actions">
        <input type="submit" id="submit" name="op" value="Submit" class="form-submit"/
    </div>

</form>
还有一点。你的代码在这里

if( ( /placeholder\.png$/.test( $('#img').attr('src') ) ) {
        return false;
}


您正在onerror事件上加载相同的映像,这将尝试加载相同的映像并再次触发onerror事件,并导致您进入无限循环。

从当前示例来看,这应该足够了:

<img id="img" src="../static/img/placeholder.png" onerror="this.src = '../static/img/placeholder.png'" alt="submitted image" />

请参见此处更详细的示例:

根据您当前的示例,这应该足够了:

<img id="img" src="../static/img/placeholder.png" onerror="this.src = '../static/img/placeholder.png'" alt="submitted image" />


请参阅此处更详细的示例:

您确认了$('img').attr('src')的内容了吗?还有,为什么这在while循环中?使用if而不是while。该
while
语句应该是
if
。实际上没有什么区别。
我的代码犯了什么错误?
可能应该说
错误
。我们可以看到提交时调用验证函数的代码吗。您确认了$('#img').attr('src')的内容了吗?还有,为什么这在while循环中?使用if而不是while。该
while
语句应该是
if
。实际上,这没有什么区别。
我的代码犯了什么错误?
可能应该说
错误
。我们可以看到提交时调用验证函数的代码吗?我不认为它会创建无限循环。当用户输入不正确的url时,占位符将被保留。基本上,我希望表单不要提交,直到一个合适的图像取代了占位符。另外,我是JS新手,来自Python bckgrd。为什么不建议JS使用while?
while
通常用于循环遍历条件语句。但是,您的代码只需要执行语句
return false
一次,如果
语句足够了,我认为它不会创建无限循环。当用户输入不正确的url时,占位符将被保留。基本上,我希望表单不要提交,直到一个合适的图像取代了占位符。另外,我是JS新手,来自Python bckgrd。为什么不建议JS使用while?
while
通常用于循环遍历条件语句。但是,您的代码只需要执行语句
return false
一次,如果
语句就足够了。这不起作用-当我使用alert函数检查它时-我收到一条消息说它是Undefinedalert($('img').attr('src'));我的javascript在一个单独的文件中-这就是为什么它不起作用的原因,我确实导入了该文件。我想这是因为我的提交按钮更改了图像,然后提交了它,所以我的代码有问题-此代码是对的。这不起作用-当我使用警报函数检查它时-我收到一条消息说它是未定义的($(“#img').attr('src')),我的javascript在一个单独的文件中-这就是为什么它不工作的原因,我确实导入了文件。我想这是因为我的提交按钮更改了图像,然后提交了它,所以我的代码有问题-这段代码是正确的。