5

我是 javascript 新手,有一些 PHP 经验,主要使用 HTML 和 css 样式。我从 Eric Martin 的网站下载了 simplemodal 联系表格,看起来非常不错。我能够在这个位置的测试网站中实现模态弹出联系表:

http://www.patagonia-tours.net/tours/patagonia.htm

在此页面中,我列出了三个不同的旅行,我希望访问者对每个旅行都进行询问/提问,为此我添加了模态表单。

我需要解决两件事:

1号

将一个变量从 HTML 传递到 JAVASCRIPT,该变量将作为旅游名称,使用此变量作为联系表单的标题。我弄清楚了这个变量在contact.js文件中的位置,并被命名为'title'。

这是调用模态表单的三个按钮的 HTML 代码:

<div id='contact-form'>
    <input type='button' name='contact' value='Ask a question / book' class='tour-button button-white' title='Tour: El Calafate / Ushuaia / Torres del Paine'>
</div> 

<div id='contact-form'>
        <input type='button' name='contact' value='Ask a question / book' class='tour-button button-white' title='Tour: Ushuaia / Australis / Paine / Calafate'>
    </div>

<div id='contact-form'>
        <input type='button' name='contact' value='Ask a question / book' class='tour-button button-white' title="Tour: Ushuaia / Puerto Madryn">

需要将 HTML 中的 title 属性的值传递给 javascript 中的 'title' 变量。

2号

我需要将相同的变量传递给 PHP 脚本 (contact.php),这样我就可以将它用作电子邮件中的主题,然后知道访问者对哪个旅游感兴趣,老实说,我现在不知道该怎么做(我确实尝试了一些变体但没有成功)。

4

1 回答 1

4

As mentioned in my comment you cannot have duplicate ID values or else javascript does not know which one to grab (as it expects only one, you stop at the first);

if you change your ID's to classes you can do something like this:

<div id='contact-form'>
    <input type='button' name='contact' value='Ask a question / book' class='tour-button contact button-white' title="Tour: Ushuaia / Puerto Madryn">
</div>

var buttons = document.getElementsByClassName('tour-button');
for( var i = 0, l = buttons.length; i < l; i++ ) {
    buttons[i].addEventListener('click', function() { alert( this.title ); }, false);
}

as for Number 2, you should ask this in a new question with the code you have tied.

edit:

Assuming you have the variable defined somewhere else as var title = ''; or just var title; then change the above code to this:

var buttons = document.getElementsByClassName('tour-button');
for( var i = 0, l = buttons.length; i < l; i++ ) {
    buttons[i].addEventListener('click', function() { title = this.title; }, false);
}

that assigns to the variable 'title' the value of the title in the last button clicked. If you are trying to do changes on the click action I suggest you also put that code in the event handler for the button clicks.

于 2013-04-18T15:29:44.103 回答