简体   繁体   English

关于this.from.“操作”的问题

[英]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:我想知道:

  1. Why it doenst put the value "all" to the artist field?为什么它不把“全部”的价值放在艺术家领域?
  2. Is this Javascript?这是 Javascript 吗? Or easy HTML?还是简单的 HTML?
  3. Is better translate this with jQuery/JS Handler or this is better?用 jQuery/JS 处理程序更好地翻译这个还是更好? (light, crossbrowsers..and so on) (光,跨浏览器..等等)

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你使用它的方式做了两个错误的假设

  1. it assumes that links inside a form have a form attribute.. They do not.它假定表单内的链接具有表单属性。它们没有。 Only input elements have a form attribute.只有输入元素具有表单属性。 Using parentNode should do the trick for you particular case since the link is a direct child of the form element.使用parentNode应该为您的特定情况解决问题,因为链接是表单元素的直接子元素。
  2. it expects a variable with name all exists and it tries to put the content of that variable in the input.它期望一个名称为 all 的变量存在,并尝试将该变量的内容放入输入中。 Making all be a string by wrapping it to single quotes ' 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/

  • If you want to use this JavaScript line then change your code with: this.form.artist.value="all"如果你想使用这个 JavaScript 线然后改变你的代码: this.form.artist.value="all"
  • Yes it is HTML with simple inline JavaScript.是的,它是 HTML,带有简单的内联 JavaScript。
  • You can use JQuery also by following changing:您也可以通过以下更改来使用 JQuery:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM