[英]Question about this.from.“operation”
I have this form:我有这个表格:
<form action="index.php?explore=browse" method="post">
<input type="hidden" name="artist" value="<?=$artist?>" />
<input type="hidden" name="event" value="<?=$event?>" />
<input type="hidden" name="date" value="<?=$date?>" />
<a onclick="this.form.artist.value=all; this.form.submit();return false" href="#">All</a>
</form>
and I'd like to know:我想知道:
Hope you can help me!希望你能帮我!
You need to change the你需要改变
this.form.artist.value=all;
to至
this.parentNode.artist.value='all';
The way you use it makes two wrong assumptions你使用它的方式做了两个错误的假设
parentNode
should do the trick for you particular case since the link is a direct child of the form element.parentNode
应该为您的特定情况解决问题,因为链接是表单元素的直接子元素。'
should do what you want.'
来使所有字符串成为您想要的。 demo at http://jsfiddle.net/gaby/vzuFD/演示在http://jsfiddle.net/gaby/vzuFD/
With jQuery you could do使用 jQuery 你可以做
<form action="index.php?explore=browse" method="post">
<input type="hidden" name="artist" value="<?=$artist?>" />
<input type="hidden" name="event" value="<?=$event?>" />
<input type="hidden" name="date" value="<?=$date?>" />
<a id="all" href="#">All</a>
</form>
and和
<script type="text/javascript">
$(function(){
$('#all').click(function(){
$(this)
.closest('form')
.find(':input[name=artist]')
.val('all')
.end()
.submit();
});
});
</script>
demo at http://jsfiddle.net/gaby/vzuFD/1/演示在http://jsfiddle.net/gaby/vzuFD/1/
this.form.artist.value="all"
this.form.artist.value="all"
First change your HTML Code with the following:首先使用以下代码更改您的 HTML 代码:
<form action="index.php?explore=browse" method="post">
<input type="hidden" id="artist" name="artist" value="<?=$artist?>" />
<input type="hidden" name="event" value="<?=$event?>" />
<input type="hidden" name="date" value="<?=$date?>" />
<a id="linkAll" href="#">All</a>
</form>
Then use the following jQuery:然后使用下面的 jQuery:
$('#linkAll').click( function(){
$('#artist').val('All');
});
all
is a reference to a variable that does not exist. all
是对不存在的变量的引用。 'all'
is a string containing the text "all". 'all'
是一个包含文本“all”的字符串。
Additionally, you assume that this.form
exists (but it likely doesn't).此外,您假设
this.form
存在(但它可能不存在)。 You could use parentNode
instead, but this may stop working if you move the <a>
tag.您可以改用
parentNode
,但如果您移动<a>
标签,这可能会停止工作。
So:所以:
<form action="index.php?explore=browse" method="post">
<input type="hidden" name="artist" value="<?=$artist?>" />
<input type="hidden" name="event" value="<?=$event?>" />
<input type="hidden" name="date" value="<?=$date?>" />
<a onclick="this.parentNode.artist.value='all'; this.parentNode.submit();return false" href="#">All</a>
It's usually preferred not to write onclick
handlers inside HTML, instead writing all your Javascript elsewhere in a dedicated Javascript block/file.通常最好不要在 HTML 内编写
onclick
处理程序,而是将所有Javascript 写入专用 Javascript 其他地方的其他地方。 It keeps things nice and separated.它使事情保持良好和分离。
We also prefer e.preventDefault
to return false
, here, though that's a little trickier to make cross-browser so I'll leave it for another question.)我们也更喜欢
e.preventDefault
在这里return false
,尽管跨浏览器有点棘手,所以我把它留给另一个问题。)
Here's an example demonstrating an overall better solution:这是一个展示整体更好解决方案的示例:
<form action="index.php?explore=browse" method="post" id="theForm">
<input type="hidden" name="artist" value="<?=$artist?>" />
<input type="hidden" name="event" value="<?=$event?>" />
<input type="hidden" name="date" value="<?=$date?>" />
<a href="#" id="theLink">All</a>
</form>
<script type="text/javascript">
var theLink = document.getElementById('theLink');
var theForm = document.getElementById('theForm');
theLink.onclick = function() {
theForm.artist.value = 'all';
theForm.submit();
return false;
};
</script>
A version of this with the use of jQuery might look like:使用 jQuery 的版本可能如下所示:
<script type="text/javascript">
var $theLink = $('#theLink');
var $theForm = $('#theForm');
$theLink.click(function() {
$theForm.find('[name=artist]').val('all');
$theForm.submit();
return false;
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.