首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问表单外部的链接按钮的值,并通过post方法将其传递到表单中?

如何访问表单外部的链接按钮的值,并通过post方法将其传递到表单中?
EN

Stack Overflow用户
提问于 2022-11-25 08:59:51
回答 1查看 15关注 0票数 0

这是两个选择。我想选择一个按钮链接,并必须通过post方法获得值时,我提交的表格请,我如何去做呢?若要捕获具有数据值以供出租的链接的值,请单击数据值以供出租,而对于待售的数据值,反之亦然,然后当我单击submit按钮时,我希望捕获该值并将其与搜索按钮相关联。

代码语言:javascript
复制
 <div class="mxw-670 position-relative z-index-2">
                <input class="search-field" type="hidden" name="status" value="for-sale" data-default-value="">
                <ul class="nav nav-pills property-search-status-tab">
                    <li class="nav-item" role="presentation">
                        <a class="nav-link btn shadow-none rounded-bottom-0 fs-13 letter-spacing-087 bg-dark-opacity-05 text-white hover-white text-uppercase bg-active-primary active"
                            data-toggle="pill" data-value="for-sale" href="#" role="tab" aria-selected="true" form="message">
                            sale
                        </a>
                    </li>
                    <li class="nav-item" role="presentation">
                        <a class="nav-link btn shadow-none rounded-bottom-0 fs-13 letter-spacing-087 bg-dark-opacity-05 text-white hover-white bg-active-primary text-uppercase"
                            data-toggle="pill" data-value="for-rent" href="#" role="tab" aria-selected="false" form="message">
                            rent
                        </a>
                    </li>
                </ul>
                <form action="" method="post" id="message" class="d-flex">
                    <div class="position-relative w-100">
                        <i class="far fa-search text-dark fs-18 position-absolute pl-4 pos-fixed-left-center"></i>
                        <input type="text"
                            class="rounded-bottom-right-lg w-100 pl-8 py-4 bg-white border-0 fs-13 font-weight-500 text-gray-light rounded-0 lh-17"
                            placeholder="Enter an address, neighborhood" name="search">
                    </div>
                    <button type="submit" name="submit"  class="btn btn-primary fs-16 font-weight-600 rounded-left-0 rounded-lg">
                        Search
                    </button>
                </form>
            </div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-25 09:09:55

将按钮设置为input type button,并使用jQuery Ajax调用提交表单

代码语言:javascript
复制
<input type="button" id="submitButton" name="submit"  class="btn btn-primary fs-16 font-weight-600 rounded-left-0 rounded-lg" value="Search" />

在你的javascript代码中

代码语言:javascript
复制
$('input#submitButton').click( function() {
    $.post( 'some-url', $('form#myForm').serialize(), function(data) {
         // ... do something with the response from the server
       },
       'JSON' // I expect a JSON response
    );
});

$('input#submitButton').click( function() {
    $.ajax({
        url: 'some-url',
        type: 'post',
        dataType: 'json',
        data: $('form#myForm').serialize(),
        success: function(data) {
                   // ... do something with the data...
                 }
    });
});

data调用的ajax字段中,可以使用$(element).attr('data-value')添加具有data-value属性的链接的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74570326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档