我可以索引javascript数组以匹配php变量的一组结果

我将搜索结果发送到jquery窗口 – 确切地说是10。 如果用户选择第4个结果,我希望将该特定结果分配给javascript变量。 接下来代码的底部是我认为我需要以某种方式索引javascript变量? 然后在这段代码的顶部,我想我可以分配用户选择的号码。 我已经弄明白了如何获取用户选择的数字。 同样在下面代码的底部,您将看到我在哪里注释了我的索引javascript变量以匹配相同的php变量的尝试。

 $(function() { $( "#dialog" ).dialog({height: 550, width: 450}); $( ".submit" ).click(function(){ //if(this.id.indexOf('select')>-1) {var id = (this.id.split(" "))[1]; console.log(id);} //bookSelect = id; $.ajax({ type: "POST", url: 'book-meta.php', async:true, dataType: 'json', //assign values to the variables to be passed to the server via data data: { B : B, cover : cover, title : title, author : author, published : published, ISBN : ISBN, description : description, pages : pages, publisher : publisher}, success: function(data) { //identify the variables for unique handing on the server side, this is //how the book data gets to the input fields for bbpress forums $("input[name='bookCover']").val(data.cover); $("input[name='bbp_topic_title']").val(data.title); $("input[name='bookAuthor']").val(data.author); $("input[name='bookPublished']").val(data.published); $("input[name='bookDescription']").val(data.description); $("input[name='bookPages']").val(data.pages); $("input[name='bookPublisher']").val(data.publisher); $("input[name='bookISBN']").val(data.ISBN); //$("input[name='selection']").val(data.bookSelect); //alert(B); }, error: function(errorThrown){ alert('error'); }, }); $( "#dialog" ).dialog( "close" ); }); });  

Top 10 Results for ""

choose a book to select as your topic

  $( ".submit" ).click(function(){ if(this.id.indexOf('select')>-1) {var id = (this.id.split(" "))[1]; console.log(id);} B = id; }); //for(var i = 0, len = 11; i < len; i++) //var title = []; var cover = ; //var title[i] = ; var title = ; var author = ; var published = ; var description = ; var pages = ; var publisher = ; var ISBN = ;

如果有人搜索出来,我找到了自己问题的答案。 两个警告:1)这个答案看起来很混乱。 可能有一种更简单的方法。 2)@elclanrs的评论是正确的; 我打算将此代码清理为单独的文件。 如果你看一下上面的问题,我修改了底部脚本,并修改了顶部脚本中的变量。 这是底部脚本(我使用了一个javascript foreach数组,并将所有可能的结果放在数组中 – 可能有一种方法可以做到这一点,而无需将它们全部列出来?)。 为简洁起见,我只包括前两个变量数组。

   

以下是如何在顶级脚本中分配变量:

  $.ajax({ type: "POST", url: 'book-meta.php', async:true, dataType: 'json', //assign values to the variables to be passed to the server via data, according to their B position //within the foreach array. data: { B : B, cover : b_cover[B], title : b_title[B], author : b_author[B], published : b_published[B], ISBN : b_ISBN[B], description : b_description[B], pages : b_pages[B], publisher : b_publisher[B]}, success: function(data) 
Author:
Published:
Page(s):
Publisher:
Category:    ISBN:

<?php echo ""; ?>

<img src="https://stackoverflow.com/questions/23861710/can-i-index-javascript-array-to-match-a-set-of-results-of-php-variables/" />

Description: