dtwzwmv87399 2014-10-19 06:02
浏览 33
已采纳

将动态变量传递给jquery语句

so I am trying to post and link to a dynamic URL and am using a jquery redirect to do so. I know this is probably not the best way, and I personally hate being redirected, but it works. Regardless, I am having trouble passing the dynamic url, which is generated based on database values for whichever marker is clicked on, into the jquery post form. Right now, it brings in the last value in the name and id of the last marker in my database, which makes sense to me, but is certainly not what I want to happen.

Here is my javascript code:

downloadUrl("phpsqlajax_genxml1.php", function (data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
    var id = markers[i].getAttribute("id");
    var name = markers[i].getAttribute("name");
    var description1 = markers[i].getAttribute("description");
    var type = markers[i].getAttribute("type");
    var point = new google.maps.LatLng(
            parseFloat(markers[i].getAttribute("lat")),
            parseFloat(markers[i].getAttribute("lng")));
    var url = "markerpages.php?name=" + name + "&id=" + id;
    var html = "<b>" + name + "</b> <br/>" + description1;
    var contentString = '<div id="content">' +
            '<div id="siteNotice">' +
            '</div>' +
            '<h1 id="firstHeading" class="firstHeading">' + name + '</h1>' +
            '<div id="bodyContent">' +
            '<p>' + description1 + '</p>' + 
            '<p><a href="#" id="markerpages-link">link to dynamic PHP page</a>' +
            '</p>' +
            '</div>' +
            '</div>';  

Here is my jquery code:

$(document).on("click", "a", "markerpages-link", function () {
        var form = $('<form action="' + url + '" method="post">' +
                '<input type="hidden" name="api_url" value="' + url + '" />' +
                '<input type="text" name="description2" value="' + description1 + '" />' +
                '</form>');
        $('body').append(form);  // This line is not necessary, maybe for IE
        $(form).submit();
    });

Thank you for any and all help. It is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongyuji7309 2014-10-19 07:07
    关注

    The for loop in your javascript code is going to leave the url variable set to the last marker in the markers array, as you've noted. You seem to have cut off the last part of your for loop so I'm not sure what else you're doing with the variables you set, but I assume you're somehow adding them to the DOM.

    The best thing to do would be to set some values as attributes in the DOM elements themselves, and have an event handler for the click to look for those values and do the appropriate POST submission accordingly.

    Here is an example...

    When you add your DOM elements, assign its href attribute to be the url for that marker.

    Then your event handler can be something like:

    $('a.markerpages-link').click(function(el){
        var form = $('<form action="' + el.attr('href') + '" method="post">' +
            '<input type="hidden" name="api_url" value="' + url + '" />' +
            '<input type="text" name="description2" value="' + description1 + '" />' +
            '</form>');
        $('body').append(form);  // This line is not necessary, maybe for IE
        $(form).submit();
        return false; // so that the link is not followed as it normally would
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器