获取最近的文本框到表格中的单选按钮

时间:2015-06-22 21:35:43

标签: javascript jquery html

我无法获得最接近我所选单选按钮的文本框。这是我到目前为止所尝试过的,一直让我回归未定义的。

HTML:

library(rvest)
library(stringr)
library(dplyr)
library(pbapply)

url <- "https://incidentreports.uchicago.edu/incidentReportArchive.php?startDate=06/01/2015&endDate=06/21/2015"
pg <- read_html(url)

pg %>% 
  html_nodes("li.page-count") %>% 
  html_text() %>% 
  str_trim() %>% 
  str_split(" / ") %>%
  unlist %>% 
  as.numeric %>% 
  .[2] -> total_pages

pblapply(1:(total_pages), function(j) {

  # get "column names"
  # NOTE that you get legit column names for use with "regular" 
  # data frames this way

  pg %>% 
    html_nodes("thead > tr > th") %>% 
    html_text() %>% 
    make.names -> tcols

  # get all the TDs

  pg %>% 
    html_nodes("td") %>%
    as_list() -> tds

  # how many rows do we have? (shld be 5, but you never know)

  trows <- length(tds) / 7

  # the basic idea is to grab all the TDs for each row
  # then cbind them together and then rbind the whole thing
  # while keeping decent column names

  bind_rows(lapply(1:trows, function(i) {
    setNames(cbind.data.frame(lapply(1:7, function(j) { 
      html_text(tds[[(i-1)*7 + j]])
    }), stringsAsFactors=FALSE), tcols)
  })) -> curr_tbl

  # get next url

  pg %>% 
    html_nodes("li.next > a") %>% 
    html_attr("href") -> next_url

  if (j < total_pages) {
    pg <<- read_html(sprintf("https://incidentreports.uchicago.edu/%s", next_url))
  }

  curr_tbl

}) %>% bind_rows -> incidents

incidents

## Source: local data frame [62 x 7]
## 
##                            Incident                                  Location        Reported
## 1                             Theft       1115 E. 58th St. (Walker Bike Rack) 6/1/15 12:18 PM
## 2                       Information                          5835 S. Kimbark   6/1/15 3:57 PM
## 3                       Information                  1025 E. 58th St. (Swift)  6/2/15 2:18 AM
## 4   Non-Criminal Damage to Property                850 E. 63rd St. (Car Wash)  6/2/15 8:48 AM
## 5       Criminal Damage to Property 5631 S. Cottage Grove (Parking Structure)  6/2/15 7:32 PM
## 6  Information / Aggravated Robbery                4701 S. Ellis (Public Way)  6/3/15 2:11 AM
## 7                     Lost Property           5800 S. University  (Main Quad)  6/3/15 8:30 AM
## 8       Criminal Damage to Property         5505 S. Ellis (Parking Structure) 5/29/15 5:00 PM
## 9       Information / Armed Robbery        6300 S. Cottage Grove (Public Way)  6/3/15 2:33 PM
## 10                    Lost Property                1414 E. 59th St. (I-House)  6/3/15 2:28 PM
## ..                              ...                                       ...             ...
## Variables not shown: Occurred (chr), Comments...Nature.of.Fire (chr), Disposition (chr), UCPDI. (chr)

的JavaScript / jQuery的

<fieldset class="capacity-field">
    <legend>Capacity</legend>
    <table style="margin-bottom: 20px">
        <tr>
            <td>
                <input type="radio" name="capacity" value="raw" checked>Raw (TB):
            </td>
            <td>
                <input type="text" name="raw-capacity" value="256" size="2"> TB
            </td>
        </tr>
        <tr>
            <td>
                <input type="radio" name="capacity" value="usable">Usable (TB):
            </td>
            <td>
                <input type="text" name="usable-capacity" value="161.28" size="2"> TB
            </td>
        </tr>
        <tr>
            <td>
                <input type="radio" name="capacity" value="effective">Effective (TB):
            </td>
            <td>
                <input type="text" name="effective-capacity" value="161.28" size="2"> TB
            </td>
        </tr>
    </table>

我知道此时function cap_growth_update(toUpdate) { var capacity = $("input[name='capacity']:checked").next("input[type='text']").val(); alert(capacity); } $(document).ready(function(){ cap_growth_update("T"); }); 的值是任意的,但它将在稍后用作选择器,因此包含在内。

3 个答案:

答案 0 :(得分:4)

jQuery next()函数查找兄弟元素,但由于这些元素在不同的td元素下分开,所以你必须爬上DOM:

$("input[name='capacity']:checked").closest("tr").find("input[type='text']").val();

它的作用是去最近的父,然后搜索子文本输入。

答案 1 :(得分:1)

尝试将parents()eq()find()

一起使用
$("input[name='capacity']:checked").parents().eq(1).find("input[type='text']").val();

请注意,这只是我根据您的标记提出的建议,您的目标可以通过其他方式实现(jQuery是一个用于遍历和操作DOM的丰富库)

答案 2 :(得分:0)

$(function() {
    $("input:radio").click(function() {
        if ($(this).is(":checked")) {
            var value = $(this).closest("tr").find("input:text").val();
        }
    });    
});

代码检查无线电元素上的click事件,然后检查元素是否被选中,如果已选中,则获取radio元素的父行,在行内找到输入文本并获取输入文本的值它将值保存在var值,因此您可以使用值:)

执行任何操作

问候!