正在尝试将数据属性值添加到其他页面上的输入中
Trying to add data attribute value to an input on a different page?
我有一个带有数据标题属性的操作按钮,该属性包含页面标题,当我单击此链接时,我基本上想被带到联系人页面(新页面)并用此标题填充主题输入字段,但我似乎遇到了问题。有人能提出建议吗?
$j=jQuery.noConflict();
$j(document).ready(function(){
enquiryCTA = $j('#enquiry-cta');
enquiryCTA.on('click', function(){
var dataTitle = $j(this).data('title');
$j('span.your-subject').find('input[type=text]').val(dataTitle);
});
});
HTML(张贴页面上)
<a href="http://localhost/jmccplantsales/contact-us/" id="enquiry-cta" class="right enquire-cta" data-title="HGV1300 dci">Make Enquiry</a>
HTML(在contact.php页面上)
<form action="/jmccplantsales/contact-us/#wpcf7-f118-p4-o1" method="post" class="wpcf7-form">
<div style="display: none;">
<input type="hidden" name="_wpcf7" value="118">
<input type="hidden" name="_wpcf7_version" value="3.1.1">
<input type="hidden" name="_wpcf7_unit_tag" value="wpcf7-f118-p4-o1">
<input type="hidden" name="_wpnonce" value="5ac1807bbb">
</div>
<p>Your Name (required)<br>
<span class="wpcf7-form-control-wrap your-name"><br>
<input type="text" name="your-name" value="" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" size="40"></span> </p>
<p>Your Email (required)<br>
<span class="wpcf7-form-control-wrap your-email"><br>
<input type="text" name="your-email" value="" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" size="40"></span> </p>
<p>Subject<br>
<span class="wpcf7-form-control-wrap your-subject"><br>
<input type="text" name="your-subject" value="" class="wpcf7-form-control wpcf7-text" size="40"></span> </p>
<p>Your Message<br>
<span class="wpcf7-form-control-wrap your-message"><textarea name="your-message" class="wpcf7-form-control wpcf7-textarea" cols="40" rows="10"></textarea></span> </p>
<input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit"><img class="ajax-loader" src="http://localhost/jmccplantsales/wp-content/plugins/contact-form-7/images/ajax-loader.gif" alt="Sending ..." style="visibility: hidden; ">
<div class="wpcf7-response-output wpcf7-display-none"></div>
</form>
它不会工作,因为您的javascript函数只在包含按钮的页面上执行;它不会在联系人表单的新页面上继续执行。因此,您永远不会到达更改主题字段值的行。
如果你在同一页面上有很多这样的按钮,每个按钮都有不同的主题,你可以把主题添加到HTML中每个按钮的URL中,例如
<a href="http://localhost/jmccplantsales/contact-us/?subject=HGV1300%20dci" class="right enquire-cta">Make Enquiry</a>
<a href="http://localhost/jmccplantsales/contact-us/?subject=something_else" class="right enquire-cta">Make Enquiry</a>
然而,从您的示例来看,页面上可能只有其中一个链接。我假设数据标题属性是由其他javascript动态设置的?如果是这样的话,你可以更新你的javascript函数来获得链接href并添加主题参数:
enquiryCTA.on('click', function(e){
e.preventDefault(); // Stop the link from clicking through
var dataTitle = $j(this).data('title');
var url = $j(this).attr('href') + '?subject=' + dataTitle; // get existing link href and add the subject to it
window.location = url; // load this new url instead of the one in the link href
});
无论你使用这两种方法中的哪一种,你都需要编写一些PHP来用url中subject参数的值("?subject="后面的位)填充联系人页面上的subject字段。我不是PHP开发人员,所以我不会试图给你这个代码。
希望这能帮助
Tom
编辑:我刚刚重读了你的问题,发现主题值取自当前页面的标题。在这种情况下,您不需要任何javascript,只需使用我提到的第一种方法将页面标题添加到链接href即可。
正如tomds所说:您不需要javascript,只需将页面标题添加到href中,即可将其传递给php脚本。显示这些数据的php代码只是:
<?php echo $_GET["subject"]; ?>
只需将其放在输入的value属性中的引号之间。
更新:如果您没有访问表单脚本的权限,您可以使用jQuery或javascript读取url"subject"参数,然后设置表单输入,类似于以下内容:
var subj = $.url.param("subject");
$("[name=your-subject]").val(subj);
编辑:您需要使用以下jQuery插件才能获得如上所示的主题参数:https://github.com/allmarkedup/jQuery-URL-Parser
但如果你不想使用它,这里有一个javascript函数,你可以使用它:
function getQuery(key_str) {
// return value of key_str variables query string of url
// Ex: url="index.html?alert=5&page=index"; if key_str = "alert" then it returns "5"
if(window.location.search) {
var query = window.location.search.substr(1);
var pairs = query.split("&");
for(var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
if(unescape(pair[0]) == key_str) return unescape(pair[1]);
}
return null;
}
}
然后执行:
var subj = getQuery("subject");
$("[name=your-subject]").val(subj);
- JQuery:当其他输入字段的值发生更改时更改值
- 输入类型=文件验证停止其他输入类型验证
- 使用其他输入值自动填充输入
- 创建一个计算值数组,该数组绑定到AngularJS中的其他输入
- 更新其中一个字段时刷新其他输入字段.使用Angularjs(附Plunker)
- 根据选择选项启用/禁用其他输入
- 选择列表框以更改其他输入
- 根据其他输入设置最大值
- 如何从许多其他输入中查找触发键关闭事件的特定输入框
- 基于相同形式的其他输入填充标记输入
- j查询验证模糊文本区域与其他输入问题
- 未在选择标记下拉列表中选择以隐藏其他输入元素的嵌套控制器
- HTML,Javascript,PHP - 选择菜单+其他输入框
- 当焦点从其他输入丢失时,如何聚焦主输入
- 如果其他输入已填充,如何动态添加新输入 角度
- Rails 4 / jQuery:需要在表单输入中输入值以自动填充其他输入
- 自动建议输入,防止将文本复制到其他输入文本字段
- 如何将文件名设置为其他输入的输入值
- Javascript getElementById 默认值未显示在其他输入字段中
- 选择2 多个 阻止其他输入在输入时提交表单